limsInstrumentAcqConfig.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-instrument-acq-config-edit-list-grid',
            url : limsInstrumentAcqConfig.baseUrl + '/queries/raw',
            service : 'limsInstrumentAcqConfigServiceImpl',
            dbTable : 'T_LIMS_INSTRUMENT_ACQ_CONFIG',
            columns : limsInstrumentAcqConfig.getEditListColumns(),
            deleteFormatter : function(row) {
                return row.equipCode;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'lims-instrument-acq-config-edit-list-base-info-form' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getAsyncComp('lims-instrument-acq-config-edit-list-base-info-form').done(function(form) {
                    form.refresh({
                        url : Gikam.printf(limsInstrumentAcqConfig.baseUrl + '/{id}', {
                            id : row.id
                        })
                    });
                });

            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    /*Gikam.preInsert({
                        modalTitle : 'BASEMODULE.TRM.EQUIPT.INSTRUMENTACQCONFIGS.MODAL.ADD',
                        fields : limsInstrumentAcqConfig.getBaseInfoFormFields(),
                        url : limsInstrumentAcqConfig.baseUrl
                    }).done(function(id) {
                        Gikam.getComp('lims-instrument-acq-config-edit-list-grid').refresh();
                    });*/

                    Gikam.create('modal', {
                        title : Gikam.choose.getConfig('equipment').title,
                        url : Gikam.choose.getConfig('equipment').url + Gikam.param({
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned',
                                targetTable: 'T_LIMS_INSTRUMENT_ACQ_CONFIG',
                                targetMatchColumn: 'instrumentCode',
                                thisMatchColumn: 'equipCode',
                            }))
                        }),
                        onAfterClose : function(rows) {
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            inserts = [];
                            rows.forEach(function (row) {
                                inserts.push({
                                    // equiptId : row.id,
                                    instrumentCode : row.equipCode,
                                    instrumentName : row.equipName,
                                    instrumentType : row.equipClass
                                })
                            })

                            Gikam.getComp("lims-instrument-acq-config-edit-list-grid").insert(Gikam.printf(limsInstrumentAcqConfig.baseUrl), inserts ).done(function() {
                                Gikam.getComp("lims-instrument-acq-config-edit-list-grid").refresh();
                            });
                        }
                    })
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-instrument-acq-config-edit-list-grid').deleteRows(limsInstrumentAcqConfig.baseUrl);
                }
            } ]
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-instrument-acq-config-edit-list-layout',
            renderTo : workspace.window.$dom,
            west: {
                width: '40%',
                items: [this.getGridParam()]
            },
            center: {
                items : [{
                    type : 'button',
                    text : '测试连接',
                    icon : 'submit', color: 'success',
                    onClick : function() {
                        var connect = Gikam.getComp('lims-instrument-acq-config-edit-list-base-info-form').getData();
                        if(Gikam.isEmpty(connect) || Gikam.isEmpty(connect.id)){
                            Gikam.alert('请先新增仪器配置！');
                            return;
                        }
                        workspace.window.showMask();
                        Gikam.post(limsInstrumentAcqConfig.baseUrl + '/connect', Gikam.getJsonWrapper(null, ['limsInstrumentAcqConfigServiceImpl', [connect]])).done(function(data) {
                            if (data.result){
                                Gikam.alert('连接成功！');
                            } else {
                                Gikam.alert('连接失败！') ;
                            }
                        }).always(function () {
                            workspace.window.closeMask();
                        });
                    }
                }, this.getBaseInfoTab() ]
            }
        });
    },

    getBaseInfoTab : function() {
        var _this = this;
        return {
            type : 'tab',
            panels : [ {
                title : 'BASEMODULE.TRM.EQUIPT.INSTRUMENTACQCONFIGS.TAB.BASE_INFO',
                items : [ {
                    type : 'form',
                    service : 'limsInstrumentAcqConfigService',
                    id : 'lims-instrument-acq-config-edit-list-base-info-form',
                    fields : limsInstrumentAcqConfig.getBaseInfoFormFields(),
                    onUpdated : function() {
                        var grid = Gikam.getComp('lims-instrument-acq-config-edit-list-grid');
                        grid.refreshRowById(grid.getActivedRow().id);
                    },
                } ]
            } ]
        }
    },

    init : function() {
        this.create();
    }
};
